package com.jy.app.lib.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateTimeUtils {
	/**
	 * 获取当前时间毫秒数（以1970开始计算）
	 *
	 * @return
	 */
	public static long getMilliSecond() {
		return new Date().getTime();
	}

	/**
	 * 返回yyyyMMddHHmmss字符串
	 *
	 * @return
	 */
	public static String getTimeStamp() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}

	/**
	 * 时间格式化（DateToString）
	 *
	 * @param date
	 * @param enumFmt
	 * @return
	 */
	public static String dateToString(Date date, EnumDateFmt enumFmt) {
		if (date == null) {
			return "";
		}
		switch (enumFmt) {
			case yyyyMMddHHmmss:
				return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
			case yyyyMMddHHmm:
				return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
			case yyyyMMddHH:
				return new SimpleDateFormat("yyyy-MM-dd HH点").format(date);
			case yyyyMMdd:
				return new SimpleDateFormat("yyyy-MM-dd").format(date);
			case yyyyMM:
				return new SimpleDateFormat("yyyy-MM").format(date);
			case HHmmss:
				return new SimpleDateFormat("HH:mm:ss").format(date);
			case yyMMddHHmmss:
				return new SimpleDateFormat("yy-MM-dd HH:mm:ss").format(date);
			case MMdd:
				return new SimpleDateFormat("MM-dd").format(date);
			case MMddHHmmss:
				return new SimpleDateFormat("MM-dd HH:mm:ss").format(date);
			case MMddHHmm:
				return new SimpleDateFormat("MM-dd HH:mm").format(date);
			case HHmm:
				return new SimpleDateFormat("HH:mm").format(date);
			default:
				return "";
		}
	}

	/**
	 * 返回yyyyMMddHHmmss格式的时间
	 * @param timestamp  时间戳
	 * @return
     */
	public static String getStringDate(long timestamp){
		Date date = new Date(timestamp);
		return dateToString(date,EnumDateFmt.yyyyMMddHHmmss);
	}

	public static String getStringDate(long timestamp,EnumDateFmt enumFmt){
		Date date = new Date(timestamp);
		return dateToString(date,enumFmt);
	}

	/**
	 * 时间格式化（String2Date）
	 *
	 * @param dateString
	 * @param enumFmt
	 * @return
	 * @throws ParseException
	 */
	public static Date stringToDate(String dateString, EnumDateFmt enumFmt)
			throws ParseException {
		switch (enumFmt) {
			case yyyyMMddHHmmss:
				return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
						.parse(dateString);
			case MMddHHmmss:
				return new SimpleDateFormat("MM-dd HH:mm:ss")
						.parse(dateString);
			case yyyyMMdd:
				return new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
			case HHmmss:
				return new SimpleDateFormat("HH:mm:ss").parse(dateString);
			case yyMMddHHmmss:
				return new SimpleDateFormat("yy-MM-dd HH:mm:ss").parse(dateString);
			case MMdd:
				return new SimpleDateFormat("MM-dd").parse(dateString);
			case yyyyMM:
				return new SimpleDateFormat("yyyy-MM").parse(dateString);
			default:
				return null;
		}
	}

	public static String getWeek(Date date) {
		String[] weeks = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (week_index < 0) {
			week_index = 0;
		}
		return weeks[week_index];
	}

	public static Date getTomorrowDate() {
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, 1);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		return date;
	}

	public static Date getDayAfterTomorrow() {
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE,2);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		return date;
	}

	public static Date getTodayAndAddDays(int num) {
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE,num);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果

		int year =calendar.get(Calendar.YEAR);
		int mounth =calendar.get(Calendar.MONTH);
		int day =calendar.get(Calendar.DAY_OF_MONTH);
		return date;
	}

	public static Date getDayAndAddDays(Date date,int num) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE,num);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		int year =calendar.get(Calendar.YEAR);
		int mounth =calendar.get(Calendar.MONTH);
		int day =calendar.get(Calendar.DAY_OF_MONTH);
		return date;
	}

	public enum EnumDateFmt {
		yyyyMMddHHmmss,yyyyMMddHH, yyyyMMddHHmm, yyyyMMdd,HHmm ,HHmmss, MMdd, yyMMddHHmmss,MMddHHmmss,MMddHHmm,yyyyMM
	}
	// 提供“yyyy-mm-dd”形式的字符串到Date的匹配
	public static Date getStringDate(String dateString) {
		return new Date(getMillis(dateString));
	}

	// 提供“yyyy-mm-dd”形式的字符串到毫秒的转换
	public static long getMillis(String dateString) {
		String[] date = dateString.split("-");
		return getMillis(date[0], date[1], date[2]);
	}

	// 根据输入的年、月、日，转换成毫秒表示的时间
	public static long getMillis(int year, int month, int day) {
		GregorianCalendar calendar = new GregorianCalendar(year, month, day);
		return calendar.getTimeInMillis();
	}

	public static long getMillis(String yearString, String monthString,
								 String dayString) {
		int year = Integer.parseInt(yearString);
		int month = Integer.parseInt(monthString)-1;
		int day = Integer.parseInt(dayString);
		return getMillis(year, month, day);

	}
}
